open(FH,"</Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_normal010310_chr11_pValue_adjust.txt");
my @normal=<FH>;
chomp(@normal);
close(FH);
open(FH,"</Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_tumor011010_chr11_pValue_adjust.txt");
my @tumor=<FH>;
chomp(@tumor);
close(FH);
my %normal_hash;
my %tumor_hash;
my @out;
my @out_reverse;
my $num=0;
my $num_overlap=0;
foreach my $line(@tumor){
	my @splitline=split "\t",$line;
	if(($splitline[4]+$splitline[5]+$splitline[6]+$splitline[7])<=10){
		next;
	}
		
	$tumor_hash{$splitline[1]} = $line;
}
foreach my $line(@normal){
	my @splitline=split "\t",$line;
	if(($splitline[4]+$splitline[5]+$splitline[6]+$splitline[7])<=10){
		next;
	}
		
	if($tumor_hash{$splitline[1]} != NULL){
		my @split_tumor=split "\t",$tumor_hash{$splitline[1]};
		my $methy_normal_A = $splitline[4]/($splitline[4]+$splitline[5]);
		my $methy_normal_B = $splitline[6]/($splitline[6]+$splitline[7]);
		my $methy_tumor_A = $split_tumor[4]/($split_tumor[4]+$split_tumor[5]);
		my $methy_tumor_B = $split_tumor[6]/($split_tumor[6]+$split_tumor[7]);
		if($split_tumor[11] < 0.05 && $splitline[11] < 0.05 && ((($methy_tumor_A > $methy_tumor_B) && ($methy_normal_A < $methy_normal_B)) || (($methy_tumor_A < $methy_tumor_B) && ($methy_normal_A > $methy_normal_B)))){
			$num++;
			print("$splitline[4]\t$splitline[5]\t$splitline[6]\t$splitline[7]\n$methy_normal_A\t$methy_normal_B\n$splitline[11]\t$split_tumor[11]\n");
			push(@out_reverse,"$line\t$tumor_hash{$splitline[1]}\n");
		}
		push(@out,"$line\t$tumor_hash{$splitline[1]}\n");
		$num_overlap++;
	}
}
print("$num\t$num_overlap\n");
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_chr11_overlap.txt");
print OUT @out;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_chr11_reverse.txt");
print OUT @out_reverse;
close(OUT);
